<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>

<?php
include 'header.php';
include 'menu.php';
?>



<?php


$configfile = '/etc/opt/fbc/fbc.xml';
$xml = simplexml_load_file($configfile);

$tarpath = $xml->tarpath;
$offlinepath = $xml->offlinepath;
$localip = $_SERVER['SERVER_ADDR'];



//function to get xml data,  the function is called with a name of a domain or po

function getSettings($object)

{
    $configfile='/etc/opt/fbc/fbc.xml';
    $xml = simplexml_load_file($configfile);

	$offlinepath = $xml->offlinepath;
	$tarpath = $xml->tarpath;
    //loop thru the servers, then load xml data and an array for the matching server name

    foreach ($xml->server as $s)
    {
        if ($s->name == $object)
        {
            $gwname = $s->name;
            $ipaddr = $s->ipaddr;
            $basepath = $s->basepath;
            $restorepath = $s->restorepath;

		if (substr($tarpath, -1) == "/")
		{
			$gwtarpath = $tarpath.$gwname;
		}
		else
		{
			$gwtarpath = $tarpath."/".$gwname;
		}

		if (substr($offlinepath, -1) == "/")
    	{	
        	$tarrestorepath = $offlinepath."tarrestore/".$gwname;
    	}
    	else
    	{
    		$tarrestorepath = $offlinepath."/tarrestore/".$gwname;
    	}



            $settings = array(
                "gwname" => $gwname,
                "ipaddr" => $ipaddr,
                "basepath" => $basepath,
                "restorepath" => $restorepath,
				"gwrestorepath" => $offlinepath.$gwname,
				"gwtarpath" => $gwtarpath,
				"tarrestorepath" => $tarrestorepath
            );

            // return the array to the caller

            return $settings;
        }
    }
}


function getBackups($basepath)

{
    $backuplist = shell_exec("ls -l $basepath | awk '{print $9}'");
    $backupdirs =  preg_split('/[\r\n]+/', $backuplist);

    return $backupdirs;

}

function getTars($gwtarpath)
{

    $tarlist =  shell_exec("ls -l $gwtarpath | grep -v '^d' | awk '{print $9}'");
    $tars =  preg_split('/[\r\n]+/', $tarlist);

    return $tars;
}


//Get the dom or po name from the url link

$name = $_GET[name];
//$name = 'utah';


$settings = getSettings($name);
$backlist = getBackups($settings[basepath]);
$tarlist = getTars($settings[gwtarpath]);

?>

</head>
<body>
<br>
<br>
<br>
<div style="text-align: center"><b><big>Recovery</b></big><br>
<hr>
<br>
<div style="text-align: left"><b>Set New Restore Path</b><br>
<div style="text-align: left;"><br>
<table style="text-align: left; width: 65%;" border="0" cellpadding="2"
cellspacing="2">
<tbody>
<tr><th width="230" style="text-align: left;">Current Restore Path<br></th>
	<th>Backup Directories<br></th>
	</tr>

<tr><td><?php echo $settings[restorepath];?><br></td>

<td>

<form name='setrestore' method='get' action='restore.php'>
<select name='restoredir'>
<?php
	foreach ($backlist as $backdir)
	{
	echo "<option value='".$backdir."'>".$backdir."</option>";
	}
?>
</select>
<input type='hidden' name='basep' value='<?php echo $settings[basepath]; ?>' ></input>
<input type='hidden' name='name' value='<?php echo $settings[gwname]; ?>' ></input>
<input type='hidden' name='restore' value='<?php echo $settings[gwrestorepath]; ?>' ></input>
</td></tr>
</tbody></table>
<input type='submit' value="Set Restore Path"></input>
</form><br>


<br>
<hr>
<br>
<div><b>Recover Tarred Backup</b><br>
<br>
<table style="text-align: left; width: 65%;" border="0" cellpadding="2"
cellspacing="2">
<tbody>
<tr>
	<th width="230"> Existing Tar Files<br></th>
</tr>
<tr><td>



<form name='restoretar' method='get' action='restoretar.php'>
<select name='tar'>
<?php
    foreach ($tarlist as $tarfile)
    {
    echo "<option value='".$tarfile."'>".$tarfile."</option>";
    }
?>

</select>
<input type='hidden' name='name' value='<?php echo $settings[gwname]; ?>' ></input>
<input type='hidden' name='restore' value='<?php echo $settings[gwrestorepath]; ?>' ></input>
<input type='hidden' name='gwtarpath' value='<?php echo $settings[gwtarpath]; ?>' ></input>
<input type='hidden' name='tarrestorepath' value='<?php echo $settings[tarrestorepath]; ?>' ></input>
</td></tr>
</tbody></table>

<input type='submit' value="Restore Tar File"></input>
</form><br>


<br>
<hr>
<br>
<?php
/*
Load GW Agent<br>
<br>
<div style="text-align: left;"><br>
<table style="text-align: left; width: 50%;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td>
<div style="text-align: left;">MTA currently running?</div>
</td>
<td><br>
</td>
</tr>
<tr>
<td>Restore Path</td>
<td><br></td>
</tr>
<tr>
<td>Load MTA using FBC IP?</td>
<td><br>
</td>
</tr>
<tr>
<td>Load MTA using "utah" IP?</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br>
</td>
/tr>
</tbody>
</table>
<br>
</div>
</div>
</div>

</div>
*/
?>
</body>
</html>


